//
//  NTNetUtil.m
//  SGShow
//
//  Created by fanshijian on 15-3-10.
//  Copyright (c) 2015年 fanshijian. All rights reserved.
//

#import "NTNetUtil.h"

@implementation NTNetUtil

/**
 * 数据请求
 **/
+ (void)sendRequestWithUrl:(NSString *)url
               requestName:(NSString *)requestName
                    method:(NTRequestMethod)methodType
                parameters:(id)parameters
                     block:(void (^)(NTHttpRequest *networkRequest, NSError *error))block {
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        @autoreleasepool {
            NTHttpRequest *request = [[NTHttpRequest alloc] init];
            
            // 手机型号
            NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
            NSString *iptype = [userDefaul objectForKey:@"iptype"];
            NSString *banben = [userDefaul objectForKey:@"banben"];
            NSString *temUrl = [NSString stringWithFormat:@"%@&terminal=%@&OSVersion=%@",url,iptype,banben];
            // 判断是否签名
            if([temUrl rangeOfString:@"sign2"].location !=NSNotFound)//_roaldSearchText
            {
                // 存在
            }else{
                // 不存在
                temUrl = [NSString stringWithFormat:@"%@&sign2=%@",temUrl,[Utils md5SignStr]];
                
            }
            NSString *lower = [temUrl lowercaseString];
            if ([lower rangeOfString:@"username"].location !=NSNotFound) {
                // 存在
            }else{
                // 不存在
                temUrl = [NSString stringWithFormat:@"%@&Username=%@",temUrl,[AppData share].username];
            }
            NSLog(@"1----------%@",temUrl);
            temUrl = [temUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSLog(@"2----------%@",temUrl);
            
            [request executeHttpRequest:temUrl requestName:requestName method:methodType params:parameters block:block];
        }
    });
}

/**
 * upload数据请求
 **/
+ (void)uploadRequestWithUrl:(NSString *)url
                 requestName:(NSString *)requestName
                      method:(NTRequestMethod)methodType
                  parameters:(id)parameters
                        data:(NSData *)data
                       block:(void (^)(NTHttpRequest *networkRequest, NSError *error))block {
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        @autoreleasepool {
            NTHttpRequest *request = [[NTHttpRequest alloc] init];
            [request uploadHttpRequest:url requestName:requestName method:methodType params:parameters data:data block:block];
        }
    });
}
+(void)postImgWithParam:(NSDictionary*)dic requestUrl:(NSString*)url dataArr:(NSArray*)arr block:(void (^)(NTHttpRequest *networkRequest, NSError *error))block
{
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        @autoreleasepool {
            NTHttpRequest *request = [[NTHttpRequest alloc] init];
            [request postImgWithParam:dic requestUrl:url dataArr:arr block:block];
        }
    });
}
+ (void)reLoginNum:(NSString *)phoneNum psw:(NSString *)psw complate:(void (^)(BOOL success))block {
    NSString *cid = [ NSUserDefualManager getValueByKey:@"cid"];
    NSString *url = [NSString stringWithFormat:businessLoginUrl,phoneNum,psw,cid];
    [self sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        
//        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSDictionary *userMess = dic[@"shopinfo"];
            NSDictionary *userMess111 = dic[@"scoreinfo"];
            //                [AppData share].cardno = userMess[@"cardno"];
            //                [AppData share].email = userMess[@"email"];
            //                [AppData share].img = userMess[@"img"];
            //                [AppData share].realname = userMess[@"realname"];
            //                [AppData share].usertype = userMess[@"usertype"];
            //                [AppData share].usertypestr = userMess[@"usertypestr"];
            
            /***********懒猫掌柜****************/
            [AppData share].doLogin = YES;
            [AppData share].loginPhone = phoneNum;
            [AppData share].loginPsw = psw;
            
            [AppData share].mobile = userMess[@"mobile"];
            [AppData share].username = userMess[@"username"];
            [AppData share].shoplogo = userMess[@"shoplogo"];
            [AppData share].shopname = userMess[@"shopname"];
            [AppData share].contacter = userMess[@"contacter"];
            [AppData share].shortintroduce = userMess[@"shortintroduce"];
            [AppData share].shopnote = userMess[@"shopnote"];
            [AppData share].openingend = userMess[@"openingend"];
            [AppData share].openingstart = userMess[@"openingstart"];
            [AppData share].serveramount = userMess[@"serveramount"];
            [AppData share].minSentamount = userMess[@"minSentamount"];
            [AppData share].privatekey = userMess[@"privatekey"];
            
            [AppData share].totalincome = userMess111[@"totalincome"];
            [AppData share].cashbalance = userMess111[@"cashbalance"];
            
            [NSUserDefualManager storageValue:phoneNum forKey:UserName];
            [NSUserDefualManager storageValue:psw forKey:Psw];
            [NSUserDefualManager storageValue:@"1" forKey:@"doLogin"];
            [[AppData share].baseTabVC theShoppingCartCount:userMess[@"shopcart"]];
            //登陆成功后 获取用户信息
//            [self getUserMess];
            
            block(YES);
        }else {
            [[AppData share] clearValue];
//            block(NO);
        }
    }];
}
#pragma mark - 获取个人信息
+ (void)getUserMess {
    NSString *url = [NSString stringWithFormat:GetUserMess,[AppData share].username,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:GetUserMess method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *suc = dic[@"r"];
        if ([suc isEqualToString:@"T"]) {
            NSDictionary *detail = dic[@"d"];
            NSString *noReadCount = detail[@"numunrradmessage"];
            [AppData share].baseTabVC.redLable.hidden = [noReadCount integerValue] == 0?YES:NO;
        }else {
//            [[MessShowView share] showWithMess:dic[@"m"]];
        }
    }];
}
@end


